'use strict';
const {app, assert} = require('egg-mock/bootstrap');

describe('CollectControllerTest', () => {
  let ctx;
  let mockUser;

  before(async function () {
    ctx = app.mockContext();

    mockUser = (isAdmin = false) => {
      app.mockContext({
        user: {
          name: 'yhx',
          _id: '5c7cd4f781c857816114c940',
          is_admin: isAdmin,
        },
      });
      app.mockCsrf();
    };
  });


  // 收藏文章
  it('should POST /collect/collect ok', async () => {
    mockUser();
    let data = {
      topic_id: '5ef4c2d33f9a8803f7a44d5f',
      collect_tags: '你好2 你好33'
    }

    let result = await app.httpRequest()
      .post('/collect/collect')
      .send(data);

    console.log(result.body);
    assert(result.body.status == "success");
  });


  // 取消收藏文章
  it('should POST /collect/de_collect ok', async () => {
    mockUser();
    let data = {topic_id: '5ef4c2d33f9a8803f7a44d5f'};

    let result = await app.httpRequest()
      .post('/collect/de_collect')
      .send(data);

    console.log(result.body);
    assert(result.body.status == 'success');
  });


  // 添加标签
  it('should POST /collect/createTag ok', async () => {
    mockUser();
    let data = {name: '我好123'};

    let result = await app.httpRequest()
      .post('/collect/createTag')
      .send(data);

    console.log(result.body);
    assert(result.body.status == 'success');
  });


  // 修改标签名
  it('should POST /collect/updateTagName ok', async () => {
    mockUser();
    let data = {id: '5ef5ebfc1fd66e148911887b', name: 'aa'};

    let result = await app.httpRequest()
      .post('/collect/updateTagName')
      .send(data);

    console.log(result.body);
    assert(result.body.status == 'success');
  });

  // 删除标签
  it('should POST /collect/removeTag ok', async () => {
    mockUser();
    let data = {id: '5ef5ebfc1fd66e148911887b'};

    let result = await app.httpRequest()
      .post('/collect/removeTag')
      .send(data);

    console.log(result.body);
    assert(result.body.status == 'success');
  });

  // 点击某一标签, 列出当前标签所收藏的文章
  it('should GET /collect/listTopicsInOneTag ok', async () => {
    mockUser();
    let collect_tag_id = '5ef5f0ceb712ec1523531271';

    let result = await app.httpRequest()
      .get(`/collect/${collect_tag_id}/listTopicsInOneTag`);

    console.log(result.body);
    assert(result.body.status == 'success');
  });


  // 展示用户所有标签及标签包含文章的数量
  it('should GET /collect/listTags ok', async () => {
    mockUser();

    let result = await app.httpRequest()
      .get(`/collect/listTags`);

    console.log(result.body);
    assert(result.body.status == 'success');
  });


  it('should GET /user/yinhaixiang/collections ok', async function () {
    this.timeout(60000);
    const res = await app.httpRequest().get(`/user/${encodeURI('殷海翔')}/collections`);
    assert(res.status = 200);
  });
});
